Conditions | 16 |
Paths | 15 |
Total Lines | 27 |
Code Lines | 2 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like index.js ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | "use strict"; |
||
21 | const isURL = (url, supportIncomplete/*=false*/) => |
||
22 | { |
||
23 | if (!isObject(url)) return false; |
||
|
|||
24 | |||
25 | // Native implementation in older browsers |
||
26 | if (!hasToStringTag && toString.call(url) === urlClass) return true; |
||
27 | |||
28 | if (!(href in url)) return false; |
||
29 | if (!(protocol in url)) return false; |
||
30 | if (!(username in url)) return false; |
||
31 | if (!(password in url)) return false; |
||
32 | if (!(hostname in url)) return false; |
||
33 | if (!(port in url)) return false; |
||
34 | if (!(host in url)) return false; |
||
35 | if (!(pathname in url)) return false; |
||
36 | if (!(search in url)) return false; |
||
37 | if (!(hash in url)) return false; |
||
38 | |||
39 | if (supportIncomplete !== true) |
||
40 | { |
||
41 | if (!isObject(url.searchParams)) return false; |
||
42 | |||
43 | // TODO :: write a separate isURLSearchParams ? |
||
44 | } |
||
45 | |||
46 | return true; |
||
47 | } |
||
48 | |||
59 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.